﻿using System;
using System.IO;
using System.Collections;
using System.Web.UI.WebControls ;
using DotNetNuke;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Services.Localization;
using DotNetNuke.Services.Exceptions;
using gdlmo.DNN.Modules.Article.Business;
namespace gdlmo.DNN.Modules.Article
{
	public abstract class Settings : DotNetNuke.Entities.Modules.ModuleSettingsBase
	{
		#region "Controls"

		protected System.Web.UI.WebControls.TextBox txtMaxChars;
		protected System.Web.UI.WebControls.CompareValidator valPageSize;
		protected System.Web.UI.WebControls.CompareValidator valMaxChars;
		protected System.Web.UI.WebControls.CheckBox chkPageShow;
		protected System.Web.UI.WebControls.DropDownList cboArticleManagers;		
		protected System.Web.UI.WebControls.DropDownList cboTemplates;
		protected System.Web.UI.WebControls.DropDownList cboCategory;				
		protected System.Web.UI.WebControls.TextBox txtPageSize;
		#endregion

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.cboArticleManagers.SelectedIndexChanged += new System.EventHandler(this.cboArticleManagers_SelectedIndexChanged);

		}
		#endregion

		#region Customer Function
		private void BindArticleManagers()
		{
			this.cboArticleManagers.DataTextField = "ModuleTitle";
			this.cboArticleManagers.DataValueField = "ModuleId";
			this.cboArticleManagers.DataSource = new ArticleController().GetArticleManagerModules(this.PortalId);
			this.cboArticleManagers.DataBind();
			
			this.cboArticleManagers.Items.Insert(0, new ListItem("<" + DotNetNuke.Services.Localization.Localization.GetString("Not_Specified") + ">", "0"));
		}
		private void BindCategories(int ArticleManagerModuleID)
		{
			string strTemp = string.Empty;
			this.cboCategory.Items.Clear();
			this.GetChildCategories(strTemp, 0, 0, ArticleManagerModuleID);
			this.cboCategory.Items.Insert(0, new ListItem("<" + DotNetNuke.Services.Localization.Localization.GetString("Not_Specified") + ">", DotNetNuke.Common.Utilities.Null.NullInteger.ToString()));
		}
		private void GetChildCategories(string strTemp, int Depth, int ParentID, int ArticleManagerModuleID)
		{
			string str = string.Empty;
			int num = 0;
		    CategoryController objCtlCateogry = new CategoryController();
			string str2 = string.Empty;
			ArrayList list = objCtlCateogry.GetByModules(ArticleManagerModuleID);
			foreach (CategoryInfo info in list)
			{
				num++;
				if (Depth > 0)
				{
					if (num < list.Count)
					{
						str = "├";
					}
					else
					{
						str = "└";
					}
				}
//				str2 = objCtlCateogry.FormatTreeSpace(strTemp, Depth) + str;
				this.cboCategory.Items.Add(new ListItem(str2 + info.Name, info.CategoryID.ToString()));
//				strTemp = str2;
//				this.GetChildCategories(strTemp, info.Depth + 1, info.CategoryID, ArticleManagerModuleID);
			}
		}
//		protected void BinderCategories()
//		{
//			cboCategory.DataTextField = "Name";
//			cboCategory.DataValueField = "CategoryID";
//			cboCategory.DataSource = CommonFunc.GetCategories();
//			cboCategory.DataBind();
//		}
		private void BindTemplates()
		{
			ArrayList list = new ArrayList();
			string path = this.Server.MapPath(this.TemplateSourceDirectory + "/XmlPackages/");
			if (Directory.Exists(path))
			{
				foreach (string str2 in Directory.GetFiles(path))
				{
					if (str2.EndsWith(".xml"))
					{						
						list.Add(str2.Substring(str2.LastIndexOf(@"\") + 1));
					}
				}
			}
			this.cboTemplates.DataSource = list;
			this.cboTemplates.DataBind();
		}
		#endregion

		#region "EventHandler"
		public override void LoadSettings()
		{
			try 
			{
				if (!Page.IsPostBack) 
				{
					BindArticleManagers();		
					BindTemplates();
					
					string ManagerModuleID = ModuleSettings["ArticleManagerModuleID"] !=null ? ModuleSettings["ArticleManagerModuleID"].ToString():string.Empty;
					if (this.cboArticleManagers.Items.FindByValue(ManagerModuleID) != null)
					{
						this.cboArticleManagers.Items.FindByValue(ManagerModuleID).Selected = true;
					}
					if (gdlmo.Common.StringHelper.IsNum(ManagerModuleID))
					{
						this.BindCategories(Convert.ToInt32(ManagerModuleID));						
					}
					else
					{
						this.BindCategories(0);						
					}
					string CategoryId = ModuleSettings["CategoryID"] != null ? ModuleSettings["CategoryID"].ToString() : string.Empty;
					if (this.cboCategory.Items.FindByValue(CategoryId) != null)
					{
						this.cboCategory.Items.FindByValue(CategoryId).Selected = true;
					}
					
					if (ModuleSettings["TemplateXml"]==null)
					{
						this.ModuleSettings["TemplateXml"] = "Template.xml";
					}
					if (this.cboTemplates.Items.FindByValue(ModuleSettings["TemplateXml"].ToString()) != null)
					{
						this.cboTemplates.Items.FindByValue(ModuleSettings["TemplateXml"].ToString()).Selected = true;
					}
					txtPageSize.Text = ((string)ModuleSettings["PageSize"]);
					txtMaxChars.Text = ((string)ModuleSettings["MaxChars"]);
//					cboCategory.SelectedValue = ((string)ModuleSettings["Category"]);
					chkPageShow.Checked = Convert.ToBoolean(ModuleSettings["IsPageShow"]);
				}
			} 
			catch (Exception exc) 
			{
				Exceptions.ProcessModuleLoadException(this, exc);
			}
		}

		public override void UpdateSettings()
		{
			try 
			{
				DotNetNuke.Entities.Modules.ModuleController objModules = new DotNetNuke.Entities.Modules.ModuleController();
				objModules.UpdateModuleSetting(ModuleId,"ArticleManagerModuleID", this.cboArticleManagers.SelectedValue);					
				objModules.UpdateModuleSetting(this.ModuleId, "CategoryID", this.cboCategory.SelectedValue.ToString());
				objModules.UpdateModuleSetting(this.ModuleId, "TemplateXml", this.cboTemplates.SelectedValue);
				objModules.UpdateModuleSetting(ModuleId, "PageSize", txtPageSize.Text);
				objModules.UpdateModuleSetting(ModuleId,"MaxChars", txtMaxChars.Text);				
				objModules.UpdateModuleSetting(ModuleId,"IsPageShow",chkPageShow.Checked.ToString());
				Response.Redirect(Globals.NavigateURL(), true);
			} 
			catch (Exception exc) 
			{
				Exceptions.ProcessModuleLoadException(this, exc);
			}
		}

		private void cboArticleManagers_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			this.BindCategories(Convert.ToInt32(this.cboArticleManagers.SelectedValue));
		}
		#endregion
	}
}